<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" class="addRole" @click="addclassfy">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table
        :data="goodslist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted==false" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag size="min" v-if="scope.row.cat_level==0">一级</el-tag>
          <el-tag size="min" v-else-if="scope.row.cat_level==1" type="success">二级</el-tag>
          <el-tag size="min" v-else type="danger">三级</el-tag>
        </template>
        <template slot="operate" slot-scope="scope">
          <el-button icon="el-icon-edit" type="primary">编辑</el-button>
          <el-button icon="el-icon-delete" type="danger">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页结构 -->
      <template>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 8, 10]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </template>
    </el-card>
    <!-- 添加分类 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addclassfyVisible"
      width="50%"
      @closed="addClassfyClosed"
    >
      <!-- 添加分类的表单对象 -->
      <el-form
        :model="addclassfyForm"
        :rules="addclassfyFormrules"
        ref="addclassfyRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addclassfyForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="slectedkeys"
            :options="goodslist"
            :props="classfa"
            @change="parentchanged"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addclassfyVisible = false">取 消</el-button>
        <el-button type="primary" @click="ensure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      goodslist: [],
      // 总页数
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          //   表示将当前列为模板列
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          //   表示将当前列为模板列
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          //   表示将当前列为模板列
          type: "template",
          template: "operate"
        }
      ],
      addclassfyVisible: false,
      //  添加数据表单对象
      addclassfyForm: {
        //   将要添加的表单数据对象
        cat_name: "",
        // 父级的id
        cat_pid: 0,
        // 分类的等级，默认一级分类
        cat_level: 0
      },
      // 添加表单的验证规则
      addclassfyFormrules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      },
      parentCateList: [],
      classfa: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true
      },
      slectedkeys: []
    };
  },
  created() {
    this.getgoodslist();
  },
  methods: {
    async getgoodslist() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类列表失败");
      }
      //   console.log(res.data);
      this.$message.success("获取商品分类列表成功");
      this.goodslist = res.data.result;
      this.total = res.data.total;
    },
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize;
      this.getgoodslist();
    },
    handleCurrentChange(newpagenum) {
      this.queryInfo.pagenum = newpagenum;
      this.getgoodslist();
    },
    // 点击添加分类按钮
    addclassfy() {
      this.getfatherclassfy();
      this.addclassfyVisible = true;
    },
    // 获得父级分类的数据列表
    async getfatherclassfy() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      if (res.meta.status !== 200) {
        return this.$http.message.error("获取数据失败!");
      }
      //  this.$http.message.success('获取数据成功')
      this.parentCateList = res.data;
    },
    // 选择项发生变化触发这个函数
    parentchanged() {
      console.log(this.slectedkeys);
      if (this.slectedkeys.length > 0) {
        this.addclassfyForm.cat_pid = this.slectedkeys[
          this.slectedkeys.length - 1
        ];
        // 为当前分类等级赋值
        this.addclassfyForm.cat_level = this.slectedkeys.length;
        return;
      } else {
        this.addclassfyForm.cat_level = 0;
        this.addclassfyForm.cat_pid = 0;
      }
    },
    ensure() {
      console.log(this.addclassfyForm);
      this.$refs.addclassfyRef.validate(async valid=>{
          if(!valid){
              return 
          }
        const {data:res}=await this.$http.post('categories',this.addclassfyForm)
        if(res.meta.status!==201){
           return this.$message.error('添加分类失败')
        }
        this.getgoodslist();
        this.addclassfyVisible=false
        this.$message.success('添加分类成功')
      })
    },
    addClassfyClosed() {
      this.$refs.addclassfyRef.resetFields();
      this.slectedkeys = [];
      this.addclassfyForm.cat_level = 0;
      this.addclassfyForm.cat_pid = 0;
    }
  }
};
</script>
<style lang="less" scoped>
.addRole {
  margin-bottom: 10px;
}
.el-cascader {
  width: 100%;
}
</style>